package com.macro.mall.service.impl;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.macro.mall.dto.BmsFeedBackDetail;
import com.macro.mall.dto.BmsFeedbackParam;
import com.macro.mall.mapper.BmsFeedbackMapper;
import com.macro.mall.model.BmsFeedback;
import com.macro.mall.model.BmsFeedbackExample;
import com.macro.mall.model.BmsFeedbackPic;
import com.macro.mall.model.BmsFeedbackPicExample;
import com.macro.mall.service.BmsFeedBackPicService;
import com.macro.mall.service.BmsFeedBackService;
import com.macro.mall.utils.UserUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 商品属性管理Service实现类
 * Created by macro on 2018/4/26.
 */
@Service
public class BmsFeedBackServiceImpl implements BmsFeedBackService {

    @Autowired
    private BmsFeedbackMapper bmsFeedbackMapper;

    @Autowired
    private BmsFeedBackPicService bmsFeedBackPicService;

    @Autowired
    private UserUtils userUtils;

    @Override
    public List<BmsFeedback> getList(BmsFeedbackParam bmsFeedbackParam) {
        Integer pageNum = bmsFeedbackParam.getPageNum();
        Integer pageSize = bmsFeedbackParam.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        BmsFeedbackExample example = new BmsFeedbackExample();
        BmsFeedbackExample.Criteria criteria = example.createCriteria();
        criteria.andIsDelete(0);
        if (StringUtils.isNotBlank(bmsFeedbackParam.getUserName())) {
            criteria.andUserNameLike("%" + bmsFeedbackParam.getUserName() + "%");
        }
        if (StringUtils.isNotBlank(bmsFeedbackParam.getUserContactWay())) {
            criteria.andUserContactWayLike("%" + bmsFeedbackParam.getUserContactWay() + "%");
        }
        if (StringUtils.isNotBlank(bmsFeedbackParam.getContent())) {
            criteria.andContentLike("%" + bmsFeedbackParam.getContent() + "%");
        }
        if (Objects.nonNull(bmsFeedbackParam.getCreateStartTime()) && Objects.nonNull(bmsFeedbackParam.getCreateEndTime())) {
            criteria.andCreateTimeBetween(bmsFeedbackParam.getCreateStartTime(),bmsFeedbackParam.getCreateEndTime());
        }
        example.setOrderByClause("create_time desc");
        List<BmsFeedback> bmsFeedbackList = bmsFeedbackMapper.selectByExample(example);
        return bmsFeedbackList;
    }

    @Override
    public int create(BmsFeedbackParam bmsFeedbackParam) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsFeedback bmsFeedback = new BmsFeedback();
        BeanUtils.copyProperties(bmsFeedbackParam, bmsFeedback);
        bmsFeedback.setCreateTime(new DateTime());
        bmsFeedback.setUpdateTime(new DateTime());
        bmsFeedback.setCreateUser(userId);
        bmsFeedback.setUpdateUser(userId);
        int count = bmsFeedbackMapper.insertSelective(bmsFeedback);
        List<String> picList = bmsFeedbackParam.getPicList();
        bmsFeedBackPicService.create(bmsFeedback.getId(), picList);
        return count;
    }

    @Override
    public int update(BmsFeedbackParam bmsFeedbackParam) {
        //获取当前登录用户id
        Long userId = userUtils.getUserId();
        BmsFeedback bmsFeedback = new BmsFeedback();
        bmsFeedback.setId(bmsFeedbackParam.getId());
        BeanUtils.copyProperties(bmsFeedbackParam, bmsFeedback);
        bmsFeedback.setUpdateTime(new DateTime());
        bmsFeedback.setUpdateUser(userId);
        int conut = bmsFeedbackMapper.updateByPrimaryKeySelective(bmsFeedback);

        List<String> picList = bmsFeedbackParam.getPicList();

        bmsFeedBackPicService.deleteByFeedBackId(bmsFeedbackParam.getId());
        bmsFeedBackPicService.create(bmsFeedbackParam.getId(), picList);
        return conut;
    }

    @Override
    public BmsFeedBackDetail getItem(Long id) {
        BmsFeedback bmsFeedback = bmsFeedbackMapper.selectByPrimaryKey(id);
        BmsFeedBackDetail bmsFeedBackDetail = new BmsFeedBackDetail();
        BeanUtils.copyProperties(bmsFeedback, bmsFeedBackDetail);

        BmsFeedbackPicExample example = new BmsFeedbackPicExample();
        example.createCriteria().andBmsFeedbackIdEqualTo(id);
        List<BmsFeedbackPic> bmsFeedbackPics = bmsFeedBackPicService.selectByExample(example);
        List<String> picLists;
        if (CollectionUtils.isNotEmpty(bmsFeedbackPics)) {
            picLists = bmsFeedbackPics.stream().filter(i -> Objects.nonNull(i) && StringUtils.isNotBlank(i.getPic()))
                    .map(BmsFeedbackPic::getPic).collect(Collectors.toList());
            bmsFeedBackDetail.setPicList(picLists);
        }
        return bmsFeedBackDetail;

    }

    @Override
    public int delete(List<Long> ids) {
        BmsFeedbackExample example = new BmsFeedbackExample();
        example.createCriteria().andIdIn(ids);
        Long userId = userUtils.getUserId();
        BmsFeedback updateDetail = new BmsFeedback();
        updateDetail.setUpdateUser(userId);
        updateDetail.setUpdateTime(new Date());
        updateDetail.setIsDelete((byte) 1);
        return bmsFeedbackMapper.updateByExampleSelective(updateDetail,example);
    }


}
